package com.dc.dao;

import com.dc.po.Comment;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface CommentRepository extends JpaRepository<Comment,Long> {

    //根据blog的id属性查找父评论为空的评论集合，就是findByBlogId AndParentCommentNull写法  神奇的Jpa
    List<Comment> findByBlogIdAndParentCommentNull(Long blogId, Sort sort);

    @Transactional
    @Modifying
    @Query("delete from Comment c where c.blog.id = ?1 and c.parentComment.id is not null ")
    void deleteCommentsByBlogId(Long id);

    @Transactional
    @Modifying
    @Query("delete from Comment c where c.blog.id = ?1")
    void deleteCommentsByBlogId1(Long id);

    @Transactional
    @Modifying
    @Query("delete from Comment c where c.parentComment.id = ?1")
    void deleteByParentCommentId(Long id);

    @Query("select c from Comment c")
    List<Comment> findTop(Pageable pageable);
}
